package com.wxh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wxh.constant.RedisConstant;
import com.wxh.dao.CheckGroupDao;
import com.wxh.dao.CheckItemDao;
import com.wxh.dao.SetmealDao;
import com.wxh.entity.PageResult;
import com.wxh.entity.QueryPageBean;
import com.wxh.pojo.CheckItem;
import com.wxh.pojo.Setmeal;
import com.wxh.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Value("${out_put_path}")
    String outputPath;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {

       setmealDao.add(setmeal);
       if(checkgroupIds!=null&&checkgroupIds.length>0){
           Integer setmealId = setmeal.getId();
           this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
       }
           jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
       List<Setmeal> list=this.findAll();
        //产生套餐列表静态页面
       generateSetmealListHTML(list);
        //产生套餐详情静态页面
       generateSetmealDetailHTML(list);
    }

    private void generateSetmealDetailHTML(List<Setmeal> list) {
        for(Setmeal setmeal:list){
            Map map = new HashMap();
            map.put("setmeal",this.findById(setmeal.getId()));
         generateHTML("setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }
    }
    private void generateSetmealListHTML(List<Setmeal> list) {
            Map map=new HashMap();
            map.put("setmealList",list);
            generateHTML("setmeal.ftl","setmealHTML.html",map);
    }
    private void generateHTML(String templateName,String htmlName,Map map) {
        FileWriter writer=null;
        Writer out=null;
        try {
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            Template template = configuration.getTemplate(templateName);
            writer=new FileWriter(outputPath+"\\"+htmlName);
            out=new BufferedWriter(writer);
            template.process(map,writer);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {

        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById4Detail(id);
    }

    @Override
    public List<Map> getSetmealReport() {
        return setmealDao.getSetmealReport();
    }

    private void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        if(checkgroupIds!=null&&checkgroupIds.length>0){
            for(Integer checkgroupId:checkgroupIds){
                Map<String,Integer> map=new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
